package com.plugin;

import android.app.Activity;
import android.content.Intent;

import androidx.annotation.NonNull;

import com.example.plugin_native.createFarm.CreateFarmActivity;
import com.kongzue.dialogx.DialogX;
import com.kongzue.dialogx.style.MIUIStyle;
import com.map.tiandimap.MapView;
import com.plugin.view.MapViewFactory;
import com.support.utils.SPUtils;

import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.embedding.engine.plugins.activity.ActivityAware;
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.PluginRegistry;

/**
 * 注意：如果创建的是kotlin的插件，会生成一个kotlin的文件夹，如果所有的代码都在kotlin文件夹下编码，有些.java
 * 文件会经常导致无法依赖，所以把所有代码迁移至java文件夹下，但是java文件下必须提供一个.java格式的插件定义类
 */
public class PluginNativePlugin implements FlutterPlugin, MethodChannel.MethodCallHandler, ActivityAware {

    private final int addPlotSuc = 100;

    private MethodChannel channel;
    private Activity activity;

    @Override
    public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
        //初始化天地图
        MapView.Companion.setDevKey("67086d004b94f495f0251b5bcc1ef26d");
        channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), "plugin_native");
        channel.setMethodCallHandler(this);
        //注册原生view插件
        String sign = "mapView";
        flutterPluginBinding.getPlatformViewRegistry().registerViewFactory(
                sign,
                new MapViewFactory(flutterPluginBinding.getBinaryMessenger())
        );
    }

    @Override
    public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
        channel.setMethodCallHandler(null);
    }

    @Override
    public void onAttachedToActivity(@NonNull ActivityPluginBinding binding) {
        activity = binding.getActivity();
        initParam();
        binding.addActivityResultListener((requestCode, resultCode, data) -> {
            if (requestCode == addPlotSuc) {
                if (resultCode == Activity.RESULT_OK) {
                    channel.invokeMethod("addPlotSuc", null);
                }
            }
            return false;
        });
    }

    @Override
    public void onDetachedFromActivityForConfigChanges() {

    }

    @Override
    public void onReattachedToActivityForConfigChanges(@NonNull ActivityPluginBinding binding) {

    }

    @Override
    public void onDetachedFromActivity() {

    }

    /**
     * 执行一些初始化操作
     */
    private void initParam() {
        SPUtils.INSTANCE.initSP(activity.getApplication());
        DialogX.init(activity.getApplication());
        DialogX.implIMPLMode = DialogX.IMPL_MODE.DIALOG_FRAGMENT;
        DialogX.useHaptic = true;
        DialogX.globalStyle = MIUIStyle.style();
        DialogX.globalTheme = DialogX.THEME.LIGHT;
        DialogX.onlyOnePopTip = false;
    }

    @Override
    public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result result) {
        switch (call.method) {
            case "getPlatformVersion": {
                result.success("Android ${android.os.Build.VERSION.RELEASE}");
                break;
            }
            case "goToCreateFarm": {
                activity.startActivityForResult(
                        new Intent(activity, CreateFarmActivity.class),
                        addPlotSuc
                );
                break;
            }
            default: {
                result.notImplemented();
                break;
            }
        }
    }
}
